//
//  DeviceCollectionViewCell.m
//  EZ Tracker
//
//  Created by FENGYL on 2016/11/14.
//  Copyright © 2016年 FENGYL. All rights reserved.
//

#import "DeviceCollectionViewCell.h"
#import "UIImage+RTTint.h"
#import "UserModel.h"
#import "NetworkCommand.h"
#import "MainViewController.h"

#import <SVProgressHUD.h>

@interface DeviceCollectionViewCell()

@property (strong, nonatomic) UserModel *userModel;

@property (weak, nonatomic) IBOutlet UILabel *collectDeviceName;
@property (weak, nonatomic) IBOutlet UIButton *userIconButton;
@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *activityIndicatorView;
@property (weak, nonatomic) IBOutlet UIImageView *yesImageView;
@property (weak, nonatomic) IBOutlet UILabel *serialNumberLabel;

@end

@implementation DeviceCollectionViewCell

-(UserModel *)userModel{
    
    if (!_userModel) {
        _userModel = [[UserModel alloc]init];
    }
    return _userModel;
}

-(void)setDeviceName:(NSString *)deviceName serialNumber:(NSString *)serialNumber{
    
    //头像
    for (NSDictionary *dict in self.userModel.deviceAtrrsArray) {
        
        if ([[dict allValues] containsObject:serialNumber]) {
        
            break;
        
    }
        
    }
    
    
    [self.collectDeviceName setText:deviceName];
    
    [self.serialNumberLabel setText:serialNumber];
    
    NSLog(@"%@ \n %@",self.collectDeviceName.text,serialNumber);
    
    if ([self.userModel.selectedDeviceSerialNumberArray containsObject:serialNumber]) {
        
        self.userIconButton.selected = YES;
        
    }else{
        
        self.userIconButton.selected = NO;
        
        self.yesImageView.hidden = YES;
        
    }
    
    
    //icon
//    if (self.userModel.deviceIconDictionary) {
//        
//        NSString *iconString = [self.userModel.deviceIconDictionary valueForKey:serialNumber];
//        
//        if (iconString) {
//            
//            NSData *iconData = [iconString dataUsingEncoding:NSUTF8StringEncoding];
//            
//            UIImage *iconImage = [UIImage imageWithData:iconData];
//            
//            UIImage *iconHighlightImage =[iconImage rt_darkenWithLevel:0.5];
//            
//            [self.userIconButton setImage:iconImage forState:UIControlStateNormal];
//            
//            [self.userIconButton setImage:iconHighlightImage forState:UIControlStateSelected];
//            
//        }
//        
//        
//        
//    }
    
    
}

-(void)setUpIconImage:(UIImage *)image{
    
    if (!image) {
        
        image = [UIImage imageNamed:@"ic_tracker_useractivity_yuan_2"];
        
    }
        
    [self.userIconButton setImage:image forState:UIControlStateNormal];
    
    
    //按钮selected图片变灰
    UIImage *buttonImage = image;
    
    UIImage *highlightImage = [buttonImage rt_darkenWithLevel:0.5];
    
    [self.userIconButton setImage:highlightImage forState:UIControlStateSelected];
    
}

#pragma mark 菊花控件开始动画
-(void)activityIndicatorViewStartAnimating{
    
    if ([self.userModel.deviceActivityIndicatorArray containsObject:self.serialNumberLabel.text]) {
        
        [self.activityIndicatorView startAnimating];
        
    }else{
        
        if (self.activityIndicatorView.animating) {
            
            [self.activityIndicatorView stopAnimating];
            
        }
        
        //是否有勾
        if ([self.userModel.mainViewControllerAnnotationDictionary.allKeys containsObject:self.serialNumberLabel.text]) {
            
            self.yesImageView.hidden = NO;
            
        }
        
    }
    
    
    
}

#pragma mark 点击按钮
- (IBAction)selectDevice:(id)sender {
    
    self.userIconButton.selected = !self.userIconButton.selected;
    
    MainViewController *mainViewController;
    
    for (UIView* next = [self superview]; next; next = next.superview) {
        UIResponder *nextResponder = [next nextResponder];
        if ([nextResponder isKindOfClass:[MainViewController class]]) {
            
            mainViewController = (MainViewController *)nextResponder;
            
            break;
            
        }
    }
    
    NSString *serialNumber = self.serialNumberLabel.text;
    
    if (self.userIconButton.selected && ![self.userModel.selectedDeviceSerialNumberArray containsObject:serialNumber]) {
        
        [self.userModel.selectedDeviceSerialNumberArray addObject:serialNumber];
        
        [self.userModel.deviceActivityIndicatorArray addObject:serialNumber];
        
        NSLog(@"selectedDeviceSerialNumberArray %@",self.userModel.selectedDeviceSerialNumberArray);
        
        
        //获取坐标
        [NetworkCommand getLastStatusDataForSerialNumber:self.serialNumberLabel.text Success:^(NSArray *responseObject){
            
            [self.userModel.deviceActivityIndicatorArray removeObject:serialNumber];
            
            UICollectionView *collectionView = (UICollectionView *)self.superview;
            //刷新界面
            [collectionView reloadData];
            
            
            
            //传递responseObject
            if (responseObject.count > 0) {
                
                [mainViewController addAnnotationForResponseObject:responseObject serialNumber:serialNumber deviceName:self.collectDeviceName.text];
                
            }else{
                
                [SVProgressHUD showErrorWithStatus:@"No Data."];
                
                [SVProgressHUD dismissWithDelay:1];
                
                [self.userModel.selectedDeviceSerialNumberArray removeObject:serialNumber];
                
                self.userIconButton.selected = !self.userIconButton.selected;
                
                
                
            }
            
            
            
//            MainViewController *mainViewController = (MainViewController *)collectionView.superview;
//            
//            [mainViewController addAnnotation];
            
            
        } failure:^{
            
            [self.userModel.selectedDeviceSerialNumberArray removeObject:serialNumber];
            
            [self.userModel.deviceActivityIndicatorArray removeObject:serialNumber];
            
            UICollectionView *collectionView = (UICollectionView *)self.superview;
            //刷新界面
            [collectionView reloadData];
            
            [SVProgressHUD showErrorWithStatus:@"No Data."];
            
            [SVProgressHUD dismissWithDelay:1];
            
            
            
        }];
        
    }else{
        
        [self.userModel.selectedDeviceSerialNumberArray removeObject:self.serialNumberLabel.text];
        
        [self.userModel.deviceActivityIndicatorArray removeObject:serialNumber];
        
        [mainViewController removeAnnotationFromMapViewForSerialNumber:serialNumber];
        
        //隐藏勾
        self.yesImageView.hidden = YES;
        
    }
    
    [self activityIndicatorViewStartAnimating];

    
}

-(NSString *)getSerialNumberString{
    
    return self.serialNumberLabel.text;
    
}


@end
